package com.yx.utils;

import com.yx.system.entity.SysHanwuBook;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 订单工具类，用来生成订单号
 *
 * @author xiaoyu
 */
public class OrderUtil {

    private static long orderNum = 0L;
    private static String date;

    //声明重入锁
    static ReentrantLock lock = new ReentrantLock();

    /**
     * 生成订单号
     *
     * @return
     */
    public static String getOrderNo() {
        try {
            lock.lock();
            String str = new SimpleDateFormat("yyMMddHHmm").format(new Date());
            if (date == null || !date.equals(str)) {
                date = str;
                orderNum = 0L;
            }
            orderNum++;
            long orderNo = Long.parseLong((date)) * 10000;
            orderNo += orderNum;
            return orderNo + "";
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            lock.unlock();
        }

    }


    /**
     * 生成订单号
     *
     * @return
     */
    public static String getOrderNo2() {
        /**
         * 生成订单二维码密码18位 当前时间年份+10位随机数+当前时间戳毫秒数
         *
         * @return
         */
            try {
                lock.lock();
                //当前时间戳
                long now = System.currentTimeMillis();
                //获取4位年份数字
                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy");
                //获取时间戳
                String time=dateFormat.format(now);
                String str="";
                //截取当前时间戳的毫秒数 按java毫秒时间戳计算13位 及即使13位变成14位 也只获取最后4位的毫秒数
                String millisecond = String.valueOf(now).substring(String.valueOf(now).length()-4);
                for(int i=0;i<10;i++){
                    str+=String.valueOf((int)(Math.random()*9));
                }
                //当前时间年份+10位随机数+当前时间戳毫秒数
                String times = time +  str + millisecond;
                return times;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }finally {
                lock.unlock();
            }
    }

    /**
     * @描述 java生成流水号
     * 14位时间戳 + 6位随机数
     * @参数:@return
     * @返回值：String
     */
    public static String getNumberForPK() {
        try {
            lock.lock();
            String str = new SimpleDateFormat("yyMMddHHmm").format(new Date());
            int random = (int) (Math.random() * 10000);
            return str + random;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            lock.unlock();
        }

    }

    public static String getNumberForInt() {
        try {
            lock.lock();
            String str = new SimpleDateFormat("yyMMddHH").format(new Date());
            int random = (int) (Math.random() * 100);
            return str+random;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            lock.unlock();
        }

    }

    /*
    * 生成后台账号(随机6位数字+随机三个英文字母)
    */
    public static String getAcount(){
        String arr = "";
        for (int i = 0; i < 3; i++) {
            int random = (int) (Math.random()*10);
            arr += random;
        }
        for (int i = 0; i < 3; i++) {
            char c=(char)(int)(Math.random()*26+97);
            arr += c;
        }
        for (int i = 0; i < 3; i++) {
            int random = (int) (Math.random()*10);
            arr += random;
        }
        return arr;
    }

    /*
    * 生成后台账号密码(随机6位数字)
    */
    public static String getPassword(){
        String arr = "";
        for (int i = 0; i < 3; i++) {
            int random = (int) (Math.random()*10);
            arr += random;
        }
        for (int i = 0; i < 3; i++) {
            int random = (int) (Math.random()*10);
            arr += random;
        }
        return arr;
    }


    public static void main(String[] args){
        for (int i = 0; i < 30; i++) {
            String arr = getPassword();
            System.out.println(arr);
        }

    }



}
